package ;
import zpp_nape.Config;
import zpp_nape.Const;
import zpp_nape.util.Flags;
import zpp_nape.util.Math;
import zpp_nape.util.Names;
import zpp_nape.util.Pool;
import zpp_nape.util.Lists;
import zpp_nape.shape.Circle;
import zpp_nape.shape.Edge;
import zpp_nape.space.Space;
import zpp_nape.shape.Polygon;
import zpp_nape.shape.Shape;
import zpp_nape.phys.FeatureMix;
import zpp_nape.phys.FluidProperties;
import zpp_nape.phys.Material;
import zpp_nape.phys.Body;
import zpp_nape.geom.AABB;
import zpp_nape.geom.Distance;
import zpp_nape.geom.Mat23;
import zpp_nape.geom.Vec2;
import zpp_nape.geom.VecMath;
import zpp_nape.geom.Collide;
import zpp_nape.dynamics.Arbiter;
import zpp_nape.dynamics.Contact;
import zpp_nape.dynamics.InteractionFilter;
import zpp_nape.constraint.Constraint;
import zpp_nape.dynamics.SpaceArbiterList;
import zpp_nape.constraint.Matrices;
import zpp_nape.constraint.PivotJoint;
import zpp_nape.constraint.WeldJoint;
import nape.util.Flags;
import nape.util.Lists;
import nape.util.Debug;
import nape.space.Space;
import nape.shape.Circle;
import nape.shape.Polygon;
import nape.shape.Edge;
import nape.shape.Shape;
import nape.phys.FluidProperties;
import nape.phys.Material;
import nape.phys.Body;
import nape.geom.AABB;
import nape.geom.Geom;
import nape.geom.Mat23;
import nape.geom.Vec2;
import nape.dynamics.Contact;
import nape.dynamics.Arbiter;
import nape.dynamics.InteractionFilter;
#if cpp#if swc@:keep#end class DummyNapeMain{
    static function main(){}
}
#elseif swc#if swc@:keep#end class DummyNapeMain{
    static function main(){}
}
#else#if swc@:keep#end class DummyNapeMain{
    static function main(){
        flash.Lib.current.stage.quality=flash.display.StageQuality.MEDIUM;
        var space=new Space(new Vec2(0,300));
        var debug=new Debug();
        var stat=new Body(BodyType.KINEMATIC);
        stat.shapes.add(new Circle(0.01));
        stat.space=space;
        var r=2.5;
        var d=5;
        var da=6;
        var fr=10;
        var N=2;
        var objs=[stat];
        for(i in 1...N){
            var b=new Body();
            b.position.setxy(200,10+(i+1)*d);
            b.shapes.add(new Polygon(Polygon.regular(r*2,r*2,14)));
            b.space=space;
            b.velocity.x=(i+1)*10;
            objs.push(b);
        }
        var cons=[];
        for(i in 0...objs.length-1){
            var j=i+1;
            var a=objs[i];
            var b=objs[j];
            var con=new ZPP_WeldJoint();
            con.b1=a.zpp_inner;
            con.b2=b.zpp_inner;
            if(i==0){
                con.a1localx=200;
                con.a1localy=10+d*0.5;
                {
                    #if NAPE_ASSERT var res={
                        !({
                            #if flash9 var ret:Bool=untyped __global__["isNaN"](con.a1localx);
                            ret;
                            #else Math.isNaN(con.a1localx);
                            #end
                        });
                    };
                    if(!res)throw "assert("+"!assert_isNaN(con.a1localx)"+") :: "+("vec_set(in n: "+"con.a1local"+",in x: "+"200"+",in y: "+"10+d*0.5"+")");
                    #end
                };
                {
                    #if NAPE_ASSERT var res={
                        !({
                            #if flash9 var ret:Bool=untyped __global__["isNaN"](con.a1localy);
                            ret;
                            #else Math.isNaN(con.a1localy);
                            #end
                        });
                    };
                    if(!res)throw "assert("+"!assert_isNaN(con.a1localy)"+") :: "+("vec_set(in n: "+"con.a1local"+",in x: "+"200"+",in y: "+"10+d*0.5"+")");
                    #end
                };
            };
            else{
                con.a1localx=0;
                con.a1localy=d*0.5;
                {
                    #if NAPE_ASSERT var res={
                        !({
                            #if flash9 var ret:Bool=untyped __global__["isNaN"](con.a1localx);
                            ret;
                            #else Math.isNaN(con.a1localx);
                            #end
                        });
                    };
                    if(!res)throw "assert("+"!assert_isNaN(con.a1localx)"+") :: "+("vec_set(in n: "+"con.a1local"+",in x: "+"0"+",in y: "+"d*0.5"+")");
                    #end
                };
                {
                    #if NAPE_ASSERT var res={
                        !({
                            #if flash9 var ret:Bool=untyped __global__["isNaN"](con.a1localy);
                            ret;
                            #else Math.isNaN(con.a1localy);
                            #end
                        });
                    };
                    if(!res)throw "assert("+"!assert_isNaN(con.a1localy)"+") :: "+("vec_set(in n: "+"con.a1local"+",in x: "+"0"+",in y: "+"d*0.5"+")");
                    #end
                };
            };
            {
                con.a2localx=0;
                con.a2localy=-d*0.5;
                {
                    #if NAPE_ASSERT var res={
                        !({
                            #if flash9 var ret:Bool=untyped __global__["isNaN"](con.a2localx);
                            ret;
                            #else Math.isNaN(con.a2localx);
                            #end
                        });
                    };
                    if(!res)throw "assert("+"!assert_isNaN(con.a2localx)"+") :: "+("vec_set(in n: "+"con.a2local"+",in x: "+"0"+",in y: "+"-d*0.5"+")");
                    #end
                };
                {
                    #if NAPE_ASSERT var res={
                        !({
                            #if flash9 var ret:Bool=untyped __global__["isNaN"](con.a2localy);
                            ret;
                            #else Math.isNaN(con.a2localy);
                            #end
                        });
                    };
                    if(!res)throw "assert("+"!assert_isNaN(con.a2localy)"+") :: "+("vec_set(in n: "+"con.a2local"+",in x: "+"0"+",in y: "+"-d*0.5"+")");
                    #end
                };
            };
            con.frequency=30*0;
            con.damping=20;
            space.zpp_inner.constraints.add(con);
            cons.push(con);
        }
        var md=false;
        flash.Lib.current.stage.addEventListener(flash.events.MouseEvent.MOUSE_DOWN,function(_)md=true);
        flash.Lib.current.stage.addEventListener(flash.events.MouseEvent.MOUSE_UP,function(_)md=false);
        var flu=new Body(BodyType.KINEMATIC);
        flu.shapes.add(new Polygon(Polygon.rect(0,400,600,200)));
        var s=flu.shapes.at(0);
        s.fluidEnabled=true;
        s.fluidProperties=new FluidProperties(2,0.05);
        flu.space=space;
        flu.align();
        var wall=new Body(BodyType.STATIC);
        wall.shapes.add(new Polygon(Polygon.rect(0,0,10,600)));
        wall.shapes.add(new Polygon(Polygon.rect(600,0,-10,600)));
        wall.shapes.add(new Polygon(Polygon.rect(0,600,600,-10)));
        wall.shapes.add(new Polygon(Polygon.rect(0,0,600,10)));
        wall.space=space;
        flash.Lib.current.addEventListener(flash.events.Event.ENTER_FRAME,function(_){
            var s=flash.Lib.current.stage;
            if(md){
                {
                    cons[0].a1localx=s.mouseX;
                    cons[0].a1localy=s.mouseY;
                    {
                        #if NAPE_ASSERT var res={
                            !({
                                #if flash9 var ret:Bool=untyped __global__["isNaN"](cons[0].a1localx);
                                ret;
                                #else Math.isNaN(cons[0].a1localx);
                                #end
                            });
                        };
                        if(!res)throw "assert("+"!assert_isNaN(cons[0].a1localx)"+") :: "+("vec_set(in n: "+"cons[0].a1local"+",in x: "+"s.mouseX"+",in y: "+"s.mouseY"+")");
                        #end
                    };
                    {
                        #if NAPE_ASSERT var res={
                            !({
                                #if flash9 var ret:Bool=untyped __global__["isNaN"](cons[0].a1localy);
                                ret;
                                #else Math.isNaN(cons[0].a1localy);
                                #end
                            });
                        };
                        if(!res)throw "assert("+"!assert_isNaN(cons[0].a1localy)"+") :: "+("vec_set(in n: "+"cons[0].a1local"+",in x: "+"s.mouseX"+",in y: "+"s.mouseY"+")");
                        #end
                    };
                };
            }
            else{}
            var g=flash.Lib.current.graphics;
            g.clear();
            var n=1;
            for(i in 0...n)space.step(1/60/n,10,10);
            debug.draw(flash.Lib.current.graphics,space);
            {
                var cx_ite=space.zpp_inner.constraints.begin();
                while(cx_ite!=null){
                    var con=cx_ite.elem();
                    {};
                    cx_ite=cx_ite.next;
                }
            };
        });
    }
}
#end